<template>
	<view class="offline-list" :class="[config.style]">
		<!-- 顶部固定部分 -->
		<view class="top-warp">
			<Search title="搜索组件" type="search" :keyword="keyword" />
			<view class="tabs-block">
				<view class="tabs">
					<ListTabs v-model="tabIndex" :config="config" :tabs="tabs" @change="tabChange"></ListTabs>
				</view>
				
				<view class="attribute" @click="showDrawer('showRight')">
					<image src="../../../static/images/attribute.png" mode="widthFix" class="image"></image>
					<text class="text">筛选</text>
				</view>
				
			</view>
		</view>
		<!--筛选抽屉部分-->
		<view>
			<Attribute ref="attributeDrawer" page="offline" :config="config" :urlParams="urlParams"></Attribute>
		</view>
		<!--数据列表部分-->
		<view class="list-section uni-clearfix">
			<!-- top="xxx"下拉布局往下偏移,防止被悬浮菜单遮住 -->
			<mescroll-uni ref="mescrollRef" @init="mescrollInit" top="158" :down="downOption" @down="downCallback" @up="upCallback">
				<!-- 数据列表 -->
				<view class="list rank0">
					<view class="item" v-for="(item,index) in goods" :key="index" @click="goToDetailLink(item.id)">
						<view class="content">
							<view class="cover">
								<!-- #ifdef APP-PLUS -->
								<image :lazy-load="true" :src="item.cover_400"></image>  
								<!-- #endif --> 
								 <!-- #ifdef MP -->  
								<image mode="widthFix" lazy-load="true" :src="item.cover_400" @error="imageError" class="image"></image>
								<!-- #endif -->  
								<!-- #ifdef H5 -->  
								<img class="image" v-lazy="item.cover_400">
								<!-- #endif --> 
							</view>
							<view class="info">
								<view class="title text-ellipsis-2">{{item.title}}</view>
								<view class="price-box">
									<text class="price">￥ {{item.price == 0 ? '免费':item.price}}</text>
									<text class="marking-price" v-if="config.show_marking_price==1&&item.marking_price!=0">￥ {{item.marking_price}}</text>
								</view>
								<view class="exp-info">
									<view class="item view" v-if="config.show_view == 1">
										<uni-icons type="eye" size="10" color="#999"></uni-icons> 
										<text class="num">{{item.handling_view}}</text>
									</view>
									<view class="item sales" v-if="config.show_sale == 1">
										<uni-icons type="cart-filled" size="10" color="#999"></uni-icons> 
										<text class="num">{{item.handling_sales}}</text>
									</view>
									<view class="item favorites" v-if="config.show_favorites == 1">
										<uni-icons type="star" size="10" color="#999"></uni-icons> 
										<text class="num">{{item.handling_favorites}}</text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</mescroll-uni>
		</view>
	</view>
</template>

<script>
	import { mapState,mapMutations } from 'vuex'
	import Search from '@/common/diys/Search/Search.vue' //搜索组件
	import ListTabs from "@/common/components/listTabs"; //列表tab
	import Attribute from "../common/attribute"; //筛选抽屉

	export default {
		components: {
			Search,
			ListTabs,
			Attribute,
		},
		data() {
			return {
				urlParams: {}, //url传递过来的参数
				order_field: '', // 排序类型
				order_type: '', // 排序方向
				keyword: '',
				page: 1, //当前页码
				goods: [], //列表数据
				tabs: ['默认', '浏览量', '热卖', '价格'],
				tabIndex: 0 ,// tab下标
				downOption: {
					auto: false //是否在初始化后,自动执行downCallback; 默认true
				},
				upOption:{
					// page: {
					// 	size: 10 // 每页数据的数量
					// },
					noMoreSize: 4, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					empty:{
						tip: '~ 搜索无数据 ~', // 提示
						btnText: '去看看'
					}
				},
				showSale: this.util.setShowSale(),
			}
		},
		async onLoad(option) {
			await this.$onLaunched;
			//获取url参数
			this.urlParams = option
			this.order_field = option.order_field
			this.order_type = option.order_type
		},
		created() {
			// H5端分享设置
			//#ifdef H5
			let url = window.location.href
			let shareData = {
				title : this.config.share.title + '|线下课列表',
				desc : this.config.share.desc,
				img: this.config.share.icon
			}
			this.wechat.share(shareData,url)
			//#endif
		},
		computed: {
			...mapState(['config'])
		},
		methods: {

			// 打开筛选抽屉
			showDrawer(e) {
				//触发子组件内方法
				this.$refs.attributeDrawer.$refs[e].open()
			},
			imageLoad: function(e) {
				//console.log(e.detail)
			},
			//上拉加载的回调
			upCallback: function() {
				let that = this
				let keyword = this.tabs[this.tabIndex]
				console.log(keyword)
				let data = this.urlParams
					data.page = this.page
				let url = '';
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/offline'
					data.action = 'lists'
					data.m = 'muu_classroom'
					//#endif
					//#ifdef H5
					url = 'entry/site/offline'
					data.action = 'lists'
					data.m = 'muu_classroom_plugin_offline'
					//#endif
				//获取首页数据
				this.util.request({
					url : url,
					data : data,
					success: function (res) {
						//console.log(res.data.code);
						if (res.data.code == 200) {
							let goods = res.data.data.data
							that.goods = that.goods.concat(goods);
							//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
							that.mescroll.endSuccess(goods.length);
							that.page = that.page + 1
							//console.log(that.goods.length)
						}
					}
				})
			},
			//下拉刷新
			downCallback: function() {
				let that = this
				let keyword = this.tabs[this.tabIndex]
				this.page = 1
				//console.log(keyword)
				let data = this.urlParams
					data.page = this.page
				let url = '';
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/offline'
					data.action = 'lists'
					data.m = 'muu_classroom'
					//#endif
					//#ifdef H5
					url = 'entry/site/offline'
					data.action = 'lists'
					data.m = 'muu_classroom_plugin_offline'
					//#endif
				
				//获取分页列表首页数据
				this.util.request({
					url : url,
					data : data,
					success: function (res) {
						if (res.data.code == 200) {
							that.goods = []
							that.goods = res.data.data.data
							//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
							that.mescroll.endSuccess(that.goods.length);
							that.page = 2
						}
					}
				})
			},
			//点击空布局按钮的回调
			emptyClick(){
				uni.showToast({
					title:'点击了按钮,具体逻辑自行实现'
				})
			},
			// 切换TAB
			tabChange(e) {
				if(e == 0){
					this.urlParams.order_field = this.order_field
					this.urlParams.order_type = this.order_type
				}
				if(e == 1){
					this.urlParams.order_field = 'f_view'
					this.urlParams.order_type = 'DESC'
				}
				if(e == 2){
					this.urlParams.order_field = 'f_sales'
					this.urlParams.order_type = 'DESC'
				}
				if(e == 3){
					this.urlParams.order_field = 'price'
					this.urlParams.order_type = 'ASC'
				}
				this.page = 1
				this.goods = []// 先置空列表,显示加载进度
				this.mescroll.resetUpScroll() // 再刷新列表数据
			},
			//路由至详情页
			goToDetailLink: function(id){
				let urlParams = 'id='+id
				let url = 'detail?' + urlParams
				console.log(url)
				//指定跳转地址
				uni.navigateTo({
				    url: url
				});
			},
		}
	}
</script>

<style lang="scss">
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #f8f8f8;
		min-height: 100%;
		height: auto;
	}
	
	/*顶部固定部分*/
	.top-warp {
		background-color: #ffffff;
		.tabs-block {
			display: flex;
			flex-wrap: wrap;
			margin: 0 10px;
			.tabs {
				flex: 1;
			}
			.attribute {
				width: 60px;
				text-align: center;
				padding-top: 7px;
				.image {
					display: inline-block;
					width: 16px;
					height: 16px;
					margin-right: 5px;
					vertical-align:top;
				}
				.text {
					display: inline-block;
					font-size: 12px;
					color: #666;
					vertical-align:top;
				}
			}
		}
	}

	/*横排*/
	.list.rank0 {
		/*小图*/
		padding: 0 5px;
		display: flex;
		flex-wrap: wrap;
		>.item {
			box-sizing:border-box;
			width: calc(50% - 10px);
			background-color: #fff;
			margin: 5px;
			border-radius: 5px;
			box-shadow: 0 0 5px rgba(0,0,0,.02);
			overflow: hidden;
			.content {
				.cover {
					overflow: hidden;
					.image {
						border-radius: 5px 5px 0 0;
						width: 100%;
						height: auto;
						display: block;
					}
				}
				.info {
					padding: 5px;
					.title {
						font-size: 14px;
						color: #000;
						font-weight: 600;
						height: 36px;
						line-height: 18px;
					}
					.description {
						color: #999;
						font-size: 12px;
					}
					.price-box {
						.price {
							margin-top: 10rpx;
							font-size: 24rpx;
						}
						.marking-price {
							font-size: 23rpx;
							color: #999;
							text-decoration:line-through;
							margin-left: 10rpx;
						}
					}
					.exp-info {
						margin-top: 5px;
						color: #999;
						font-size: 12px;
						.item {
							display: inline;
							margin: 0;
							.num {
								margin: 0 5px;
							}
						}
					}
				}
			}
		}
	}
	/*竖排*/
	.list.rank1 {
		/*小图*/
		display: flex;
		flex-wrap: wrap;
		.item {
			width: 100%;
			.content {
				padding: 5px;
				display: flex;
				flex-wrap: wrap;
				.cover {
					width: 140px;
					border-radius: 5px;
					overflow: hidden;
					.image {
						width: 100%;
						height: auto;
						display: block;
					}
				}
				.info {
					padding-left: 10px;
					width: calc(100% - 150px);
					.title {
						font-size: 14px;
						color: #000;
						font-weight: 600;
						height: 36px;
						line-height: 18px;
					}
					.description {
						color: #999;
						font-size: 12px;
					}
					.price-box {
						.price {
							margin-top: 10rpx;
							font-size: 24rpx;
						}
						.marking-price {
							font-size: 23rpx;
							color: #999;
							text-decoration:line-through;
							margin-left: 10rpx;
						}
					}
					.exp-info {
						margin-top: 5px;
						color: #999;
						font-size: 12px;
						.item {
							display: inline;
							.num {
								margin: 0 5px;
							}
						}
					}
				}
			}
		}
	}
	
	/*页面风格设计*/
	.Blue {
		.price {
			color: #03b8cf;
		}
	}
</style>
